package com.lkd.controller;

import com.lkd.dto.NodeDto;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.NodeService;
import com.lkd.vo.BusinessVo;
import com.lkd.vo.NodeVo;
import com.lkd.vo.VendingMachineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vm-service")
public class NodeController {

    @Autowired
    private NodeService nodeService;

    /**
     * 分页查看区域详情
     * @param pageIndex
     * @param pageSize
     * @param name
     * @param regionId
     * @return
     */
    @GetMapping("/node/search")
    public Result<PageBean<NodeVo>> findByNodePage(
            @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "regionId", required = false) String regionId
    ){
        PageBean<NodeVo> page = nodeService.findByNodePage(pageIndex,pageSize,name,regionId);
        return Result.success(page);
    }

    /**
     * 查询商圈列表
     * @return
     */
    @GetMapping("/businessType")
    public Result<List<BusinessVo>> findBusinessType(){
        List<BusinessVo> voList = nodeService.findBusinessType();
        return Result.success(voList);
    }

    /**
     * 新增点位
     * @param nodeVo
     * @return
     */
    @PostMapping("/node")
    public Result save(@RequestBody NodeVo nodeVo){
        nodeService.save(nodeVo);
        return Result.success();
    }

    /**
     * 根据点位id查询设备列表
     * @param nodeId
     * @return
     */
    @GetMapping("/node/vmList/{nodeId}")
    public Result<List<VendingMachineVo>> findById(@PathVariable(name = "nodeId") Long nodeId){
        List<VendingMachineVo> vendingMachineVoList = nodeService.findById(nodeId);
        return Result.success(vendingMachineVoList);
    }

    /**
     * 修改点位
     * @param dto
     * @return
     */
    @PutMapping("/node")
    public Result update(@RequestBody NodeDto dto){
        nodeService.update(dto);
        return Result.success();
    }

    /**
     * 删除点位
     * @param nodeId
     * @return
     */
    @DeleteMapping("/node/{nodeId}")
    public Result delete(@PathVariable(name = "nodeId") Long nodeId){
        nodeService.delete(nodeId);
        return Result.success();
    }

    /**
     * 查询点位数量
     * @return
     */
    @GetMapping("/node/count")
    public Result count(){
        int count = nodeService.count();
        return Result.success(count);
    }
}
